<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <button id="agentLevelBtnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
                        <button id="agentLevelBtnExport" class="layui-btn icon-btn"><i class="layui-icon">&#xe67d;</i>导出</button>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="agentLevelTable" lay-filter="agentLevelTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="agentLevelTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="agentLevelForm">
    <form lay-filter="agentLevelForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">渠道级别名称</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入级别名称" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">级别编号</label>
            <div class="layui-input-block">
                <input name="code" placeholder="请输入级别编号" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">级别描述</label>
            <div class="layui-input-block">
                <input name="remark" placeholder="请输入级别描述" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">渠道要求</label>
            <div class="layui-input-block">
                <textarea id="askFor" name="askFor" placeholder="请输入内容(200字以内)" class="layui-textarea" maxlength="200" ></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">渠道支持政策</label>
            <div class="layui-input-block">
                <textarea id="supportPolicy" name="supportPolicy" placeholder="请输入内容(200字以内)" class="layui-textarea" maxlength="200" ></textarea>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="agentLevelFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin',
    'layedit'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var layedit = layui.layedit;
        var upload = layui.upload;

        // 渲染表格
        var insTb = table.render({
            elem: '#agentLevelTable',
            url: config.base + 'enterprisecab/unit/admin/agentLevel/list',
            toolbar: true,
            defaultToolbar: ["filter"],
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers'}
                ,{field: 'name', title: '级别名称'}
                ,{field: 'code', title: '级别编号'}
                ,{field: 'remark', title: '级别描述'}
                ,{field: 'askFor', title: '渠道要求'}
                ,{field: '', title: '渠道主体数量'}
                ,{field: 'creatorName', title: '添加人'}
                ,{field: 'createTime', sort: true, title: '添加时间'}
                ,{align: 'center', toolbar: '#agentLevelTableBar', title: '操作', width: 200}
            ]],
            done: function (res, curr, count) {
                //移除按钮
                var menuButton = config.getMenuButton(location.hash);
                if(menuButton.indexOf("搜索") == -1){
                    $('.layui-form.toolbar').remove();
                }
                if(menuButton.indexOf("修改") == -1){
                    $("[lay-event='edit']").remove();
                }
                if(menuButton.indexOf("删除") == -1){
                    $("[lay-event='del']").remove();
                }
            }
        });

        // 工具条点击事件
        table.on('tool(agentLevelTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }
        });

        //监听排序
        table.on('sort(agentLevelTable)', function(obj){
            table.reload('agentLevelTable', {
                initSort: obj
                ,where: {
                    sort: obj.field
                    ,order: obj.type
                }
            });
        });

        // 添加按钮点击事件
        $('#agentLevelBtnAdd').click(function () {
            showEditModel();
        });

        // 显示编辑弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: '880px',
                offset: '65px',
                title: data ? '修改代理商级别表' : '添加代理商级别表',
                content: $('#agentLevelForm').html(),
                success: function () {

                    //查询企业信息
                    var enterpriseInfo;
                    admin.req('enterprise/system/admin/enterpricceId/getEnterpricceIdVO', {}, function (res) {
                        enterpriseInfo = res.data;
                    }, 'get',false);
                    if("" == enterpriseInfo || null == enterpriseInfo){
                        layer.msg("企业信息获取异常", {icon: 1});
                        return;
                    }

                    var layeditIndex13 = layedit.build('askFor',{
                        uploadImage: {
                            url: config.upload_url + 'uploadFile',
                            type: 'post',
                            data: {"path": enterpriseInfo.enterpriseCode+"/"},
                            base: config.base,
                            before: function(obj){
                                layer.load(); //上传loading
                                this.headers={Authorization:'Bearer '+config.getToken()};
                                this.data = {"path": enterpriseInfo.enterpriseCode+"/"};
                            }
                        }
                    });

                    var layeditIndex14 = layedit.build('supportPolicy',{
                        uploadImage: {
                            url: config.upload_url + 'uploadFile',
                            type: 'post',
                            data: {"path": enterpriseInfo.enterpriseCode+"/"},
                            base: config.base,
                            before: function(obj){
                                layer.load(); //上传loading
                                this.headers={Authorization:'Bearer '+config.getToken()};
                                this.data = {"path": enterpriseInfo.enterpriseCode+"/"};
                            }
                        }
                    });
                    if(data){
                        layedit.setContent(layeditIndex13, data.askFor,false);
                        layedit.setContent(layeditIndex14, data.supportPolicy,false);
                    }
                    console.log('data：',data)
                    form.val('agentLevelForm', data);
                    // 表单提交事件
                    form.on('submit(agentLevelFormSubmit)', function (d) {
                        layer.load(2);
                        d.field.askFor = layedit.getContent(layeditIndex13);
                        d.field.supportPolicy = layedit.getContent(layeditIndex14);
                        admin.req(data ? 'enterprisecab/unit/admin/agentLevel/update' : 'enterprisecab/unit/admin/agentLevel/add', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('agentLevelTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprisecab/unit/admin/agentLevel/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }

        // 导出excel
        $('#agentLevelBtnExport').click(function () {
            var checkRows = table.checkStatus('agentLevelTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

    });
</script>